//
//  CopyPanel.m
//  PurchasingMeeting
//
//  Created by zyhang on 4/7/14.
//  Copyright (c) 2014 YX. All rights reserved.
//

#import "CopyPanel.h"
#import "DataManager.h"

@interface CopyPanel()<UITextFieldDelegate>{
    NSMutableArray *shoparray_;
    NSMutableArray *shoparrayFlag_;
    UIView *copytopaste_;
    
}

@end

@implementation CopyPanel
@synthesize copyAllOrder;
@synthesize targetObjects;

- (id)initWithFrame:(CGRect)frame withCopyFlag:(BOOL)copyAll{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        self.backgroundColor = [UIColor colorWithWhite:0.25 alpha:0.25];
        
        copyAllOrder = copyAll;
        
        UIImage *img = [[UIHelper getInstance] getBundleImage:@"detail_copyback.png"];
        copytopaste_ = [[UIImageView alloc]initWithImage:img];
        copytopaste_.frame = CGRectMake(0, 0, img.size.width, img.size.height);
        copytopaste_.center = CGPointMake(512, 300);
        copytopaste_.userInteractionEnabled = YES;
        [self addSubview:copytopaste_];
        
        UIButton *paste = [UIButton buttonWithType:UIButtonTypeCustom];
        [paste setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu.png"] forState:UIControlStateNormal];
        img = [[UIHelper getInstance] getBundleImage:@"detail_paste.png"];
        paste.frame = CGRectMake(0, 0, img.size.width, img.size.height);
        paste.center = CGPointMake([[UIHelper getInstance] getBundleImage:@"detail_copyback.png"].size.width/2-100, 310);
        [paste setTitle:@"粘贴" forState:UIControlStateNormal];
        [paste addTarget:self action:@selector(pasteclick:) forControlEvents:UIControlEventTouchUpInside];
        [copytopaste_ addSubview:paste];
        
        UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancel setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu.png"] forState:UIControlStateNormal];
        img = [[UIHelper getInstance] getBundleImage:@"detail_cancel.png"];
        cancel.frame = CGRectMake(0, 0, img.size.width, img.size.height);
        cancel.center = CGPointMake([[UIHelper getInstance] getBundleImage:@"detail_copyback.png"].size.width/2+100, 310);
        [cancel setTitle:@"取消" forState:UIControlStateNormal];
        [cancel addTarget:self action:@selector(cancelclick) forControlEvents:UIControlEventTouchUpInside];
        [copytopaste_ addSubview:cancel];
        
        shoparrayFlag_ = [NSMutableArray array];
        shoparray_ = [[NSMutableArray alloc]init];
        ShopObject *shopobj = [[DataManager sharedManager]getCurrentShop];
        NSArray *shops = [[DataManager sharedManager] getWrittenShopList];
        for (int i = 0; i<shops.count; i++) {
            ShopObject *copyshop = [shops objectAtIndex:i];
            if(![shopobj.shopName isEqualToString:copyshop.shopName]){
                [shoparray_ addObject:copyshop];
                [shoparrayFlag_ addObject:[NSNumber numberWithBool:NO]];
            }
        }
        UIScrollView *copyscrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 70, 400, 200)];
        img = [[UIHelper getInstance] getBundleImage:@"detail_copyback.png"];
        copyscrollview.center = CGPointMake(img.size.width/2,img.size.height/2);
        [copytopaste_ addSubview:copyscrollview];
        for (int i = 0; i<shoparray_.count+1; i++) {
            ShopObject *copyshop = nil;
            if (i>0) {
                copyshop = [shoparray_ objectAtIndex:i-1];
            }
            
            UIView *shopview = [[UIView alloc]initWithFrame:CGRectMake(2, i*50, copyscrollview.width-4, 50)];
            shopview.layer.borderWidth = 1;
            shopview.layer.borderColor = [UIColor grayColor].CGColor;
            [copyscrollview addSubview:shopview];
            
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 230, 30)];
            label.center = CGPointMake(150, 25);
            label.left = 10;
            label.textAlignment = NSTextAlignmentCenter;
            if (i>0) {
                label.text = [NSString stringWithFormat:@"%@  %@",copyshop.shopID,copyshop.shopName];
            }else{
                label.text = [NSString stringWithFormat:@"店铺名称"];
            }
            
            label.adjustsFontSizeToFitWidth = YES;
            [shopview addSubview:label];
            
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(label.right+10, 1, 1, 50)];
            line.backgroundColor = [UIColor grayColor];
            [shopview addSubview:line];
            
            UIButton *select = [UIButton buttonWithType:UIButtonTypeCustom];
            UIImage *img = [UIImage imageNamed:@"detail_shopunselect.png"];
            [select setImage:img forState:UIControlStateNormal];
            select.frame = CGRectMake(0, 0, img.size.width+20, img.size.height+20);
            select.center = CGPointMake(318, shopview.height/2);
            select.left = line.right + 10;
            select.tag = 10 + i-1;
            [select addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            [shopview addSubview:select];
            copyscrollview.contentSize=CGSizeMake(copyscrollview.width, 50*(shoparray_.count+1));
            if (i==0) {
                //select.hidden = YES;
                [select setImage:nil forState:UIControlStateNormal];
                [select setTitle:@"选中" forState:UIControlStateNormal];
                [select setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                select.userInteractionEnabled = NO;
            }
            
            line = [[UIView alloc]initWithFrame:CGRectMake(select.right+10, 1, 1, 50)];
            line.backgroundColor = [UIColor grayColor];
            [shopview addSubview:line];
            
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];
            [shopview addSubview:textField];
            textField.textAlignment = NSTextAlignmentCenter;
            textField.text = @"1";
            textField.tag = 100+i-1;
            textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            textField.delegate = self;
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.left = select.right+ 10;
            if (i==0) {
                textField.userInteractionEnabled = NO;
                textField.text = @"复制倍数";
            }
        }

    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)cancelclick{
    [self removeFromSuperview];
}

- (void)pasteclick:(UIButton*)sender{
//    [shoparray_ removeAllObjects];

    UserObject *user = [[DataManager sharedManager] getUserObject];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i<shoparray_.count; i++) {
//        UIButton *btn = (UIButton *)[copytopaste_ viewWithTag:10+i];
        NSNumber *flag = [shoparrayFlag_ objectAtIndex:i];
        if (![flag boolValue]) {
            [arr addObject:@"0"];
        }
        else {
            [arr addObject:@"1"];
        }
    }
    for (int i = 0; i<arr.count; i++) {
        if ([[arr objectAtIndex:i] isEqualToString:@"1"]) {
            ShopObject *shopobj = [shoparray_ objectAtIndex:i];
            ShopObject *currentshop = [[DataManager sharedManager]getCurrentShop];
            
            
            UITextField *f = (UITextField *)[copytopaste_ viewWithTag:100+i];
            int t = [f.text intValue];
            if (t==0) {
                t=1;
            }
            
            if (copyAllOrder==YES) {
                [[DataManager sharedManager]Copyorder:user.userCode
                                                 code:nil
                                           FromShopid:currentshop.shopID
                                             ToShopid:shopobj.shopID
                                             withTime:t];
            }
            else
            {
                [[DataManager sharedManager]Copyorder:user.userCode
                                                 code:targetObjects
                                           FromShopid:currentshop.shopID
                                             ToShopid:shopobj.shopID
                                             withTime:t];
            }
        }
    }
    
    [self removeFromSuperview];
}


-(void)click:(UIButton *)sender{
    UIButton *btn = sender;
    NSNumber *flag=  [shoparrayFlag_ objectAtIndex:sender.tag-10];
    if(![flag boolValue]){
        UIImage *img = [UIImage imageNamed:@"detail_shopselect.png"];
        [btn setImage:img forState:UIControlStateNormal];
//        btn.frame = CGRectMake(0, 0, btn.width, btn.height);
//        btn.center = CGPointMake(322, 22);
        
    }
    else{
        UIImage *img = [UIImage imageNamed:@"detail_shopunselect.png"];
        [btn setImage:img forState:UIControlStateNormal];
//        btn.frame = CGRectMake(0, 0,  btn.width, btn.height);
//        btn.center = CGPointMake(318, 25);
    }
    [shoparrayFlag_ replaceObjectAtIndex:sender.tag-10 withObject:[NSNumber numberWithBool:![flag boolValue]]];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField becomeFirstResponder];
    return YES;
}
@end
